﻿using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDto.ConsumptionHistoryDtos;
using CozeDto.ProjectDtos;
using CozeDto.ProjectInstanceDtos;
using D.UtilFreesqlOrm;


namespace CozeDomain.Repositories
{
    public class ProjectInstanceEntityRepository : BaseRepository<ProjectInstanceEntity>, IProjectInstanceEntityRepository
    {
        public ProjectInstanceEntityRepository(IFreeSql db) : base(db)
        {
        }


        public async Task<Tuple<long, List<RepProjectInstanceInfo>>> QueryProjectInstancePagesAsync(ReqQueryProjectInstancePage reqQueryProjectInstancePage)
        {

            var where = _db.Select<ProjectInstanceEntity, ProjectEntity, TenantsEntity>()
                                                               .LeftJoin((a, b, c) => a.ProjectId == b.Id)
                                                               .LeftJoin((a, b, c) => a.TenantId == c.Id);

            var dbres = await where.Count(out var totalCount)
                                     .Page(reqQueryProjectInstancePage.PageNo, reqQueryProjectInstancePage.PageSize)
                                     .ToListAsync((a, b, c) => new RepProjectInstanceInfo()
                                     {
                                         TenantName = c.Name,
                                         Name = b.Name,
                                         Category = b.Category,
                                         BotId = b.BotId,
                                         SinglePrice = b.SinglePrice,
                                         ExperiencePrice = b.ExperiencePrice,
                                         MonthlyCardPrice = b.MonthlyCardPrice,
                                         CreateTime = a.CreateTime,
                                         CreateUser = a.CreateUser,
                                         Description = b.Description,
                                         Id = a.Id,
                                         UpdateTime = a.UpdateTime,
                                         UpdateUser = a.UpdateUser
                                     });

            return new Tuple<long, List<RepProjectInstanceInfo>>(totalCount, dbres);
        }

        public async Task<List<RepProjectInfo>> QueryProjectListAsync(ReqQueryProjectInstanceList reqQueryProjectInstanceList)
        {
            return await _db.Select<ProjectInstanceEntity, ProjectEntity, TenantsEntity>()
                                                             .LeftJoin((a, b, c) => a.ProjectId == b.Id)
                                                             .LeftJoin((a, b, c) => a.TenantId == c.Id)
                                                             .WhereIf(!string.IsNullOrEmpty(reqQueryProjectInstanceList.CreateUser), (a, b, c) => b.CreateUser == reqQueryProjectInstanceList.CreateUser)
                                                             .WhereIf(!string.IsNullOrEmpty(reqQueryProjectInstanceList.RealmName), (a, b, c) => c.RealmName == reqQueryProjectInstanceList.RealmName)
                                                             .WhereIf(!string.IsNullOrEmpty(reqQueryProjectInstanceList.Source), (a, b, c) => b.Source == reqQueryProjectInstanceList.Source)
                                                             .WhereIf(!string.IsNullOrEmpty(reqQueryProjectInstanceList.ProjectType), (a, b, c) => b.ProjectType == reqQueryProjectInstanceList.ProjectType)
                                                             .WhereIf(!string.IsNullOrEmpty(reqQueryProjectInstanceList.Category), (a, b, c) => b.Category == reqQueryProjectInstanceList.Category)
                                                             .ToListAsync((a, b, c) => new RepProjectInfo()
                                                             {
                                                                 Category = b.Category,
                                                                 Source = b.Source,
                                                                 ProjectType = b.ProjectType,
                                                                 Flag = b.Flag,
                                                                 Description = b.Description, 
                                                                 Id = b.Id,
                                                                 Icon = b.Icon,
                                                                 Name = b.Name,
                                                             });
        }
    }
}
